﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace HundSunDBQry
{
    internal class HundsunDB
    {
        internal static List<Dictionary<string, string>> getData(string sql)
        {
            List<Dictionary<string, string>> res = new List<Dictionary<string, string>>();

            using (HttpClient client = new HttpClient())
            {
                try
                {
                    HttpContent content = new StringContent($"sql={sql}");
                    var response = client.PostAsync("http://10.0.8.35:12360/QrySqlStr", content).Result;
                    var contentstr = response.Content.ReadAsStringAsync().Result;
                    res = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(contentstr);
                    if (res == null || res.Count == 0)
                    {
                        Thread.Sleep(500);
                        response = client.PostAsync("http://10.0.8.35:12360/QrySqlStr", content).Result;
                        contentstr = response.Content.ReadAsStringAsync().Result;
                        res = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(contentstr);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                try
                {
                    if (res == null || res.Count == 0)
                    {
                        
                        Thread.Sleep(500);
                        HttpContent content = new StringContent($"sql={sql}");
                        var response = client.PostAsync("http://10.0.8.35:12360/QrySqlStr", content).Result;
                        var contentstr = response.Content.ReadAsStringAsync().Result;
                        res = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(contentstr);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            return res;
        }

        internal static WebRsp qryFundJour(string date)
        {
            WebRsp webrsp = null;
            using (HttpClient client = new HttpClient())
            {
                try
                {
                    var resstr = client.GetStringAsync($"http://10.0.8.35:12360/QryFundJour?sdate={date}&edate={date}").Result;
                    webrsp = JsonConvert.DeserializeObject<WebRsp>(resstr);
                    
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                try
                {
                    if (webrsp == null || webrsp.code != 0)
                    {
                        Thread.Sleep(500);
                        var resstr = client.GetStringAsync($"http://10.0.8.35:12360/QryFundJour?sdate={date}&edate={date}").Result;
                        webrsp = JsonConvert.DeserializeObject<WebRsp>(resstr);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                
            }
            return webrsp;
        }
    }
}
